package com.intellagric.agriecom.controller.recommend.controller;

import com.intellagric.agriecom.module.recommend.RuleServiceIN;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.pojo.RecommendTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Auther: zhy
 * @Date: 2019/5/9
 * @Description: 推荐规则模板管理
 */
@Controller
public class RecommendTemplateController {

    @Autowired
    private RuleServiceIN recommendTemplateService;


    /**
     * 添加推荐规则模板
     * @Param recommendTemplate
     * @return ResponseMessage
     */
    @RequestMapping("/recommend/template/add")
    @ResponseBody
    public ResponseMessage add(RecommendTemplate recommendTemplate) {
        recommendTemplate.setId(UUID.randomUUID().toString().replaceAll("-",""));
        recommendTemplate.setCreatedDate(new Date());
        if (recommendTemplateService.addRecommendTemplate(recommendTemplate) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }
    
    
    /**
     * 删除推荐规则模板
     * @Param id
     * @return ResponseMessage
     */
    @RequestMapping("/recommend/template/delete")
    @ResponseBody
    public ResponseMessage delete(String id) {
        if (recommendTemplateService.deleteRecommendTemplate(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }
    
    /**
     * 修改推荐规则模板
     * @Param 
     * @return ResponseMessage
     */
    @RequestMapping("/recommend/template/edit")
    @ResponseBody
    public ResponseMessage edit(RecommendTemplate recommendTemplate) {
        if (recommendTemplateService.editRecommendTemplate(recommendTemplate) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }


    /**
     * 查询推荐规则模板
     * @return RecommendTemplate
     */
    @RequestMapping("/recommend/template/{id}")
    @ResponseBody
    public RecommendTemplate get(@PathVariable String id) {
        return recommendTemplateService.queryRecommendTemplateById(id);
    }

    /**
     * 根据推荐栏位查询推荐规则模板
     * @return RecommendTemplate
     */
    @RequestMapping("/recommend/template/column/{id}")
    @ResponseBody
    public RecommendTemplate getByColumn(@PathVariable int id) {
        return recommendTemplateService.getTemplateByColumnId(id);
    }
    

    /**
     * 查询推荐规则模板列表
     * @return LayuiDataGridResult
     */
    @RequestMapping("/recommend/template/list")
    @ResponseBody
    public LayuiDataGridResult getList() {
        List<RecommendTemplate> recommendTemplateList = recommendTemplateService.queryRecommendTemplateList();
        return LayuiDataGridResult.success().add(recommendTemplateList,recommendTemplateList.size());
    }
    
    /**
     * 分页查询推荐规则模板列表
     * @return LayuiDataGridResult
     */
    @RequestMapping("/recommend/template/page")
    @ResponseBody
    public LayuiDataGridResult getPage() {
        List<RecommendTemplate> recommendTemplateList = recommendTemplateService.queryRecommendTemplateList();
        return LayuiDataGridResult.success().add(recommendTemplateList,recommendTemplateList.size());
    }

}
